<?php
/**
 * 前台用户中心页面
 * author wang
 * mail:970633164@qq.com
 */
define("PHP_IN",true);
require "include/init.php";
require ROOT_PATH."checklogin.php";
require ROOT_PATH."smarty_connection.php";

if(!empty($_POST) && $_POST['dosubmit']){
    $realname = $_POST['realname'] ;
    $sex = $_POST['sex'] ;
    $birthday = strtotime($_POST['birthday']) ;   //把日期转成时间戳
    $phone = trim($_POST['phone']) ;
    $mail = trim($_POST['mail']) ;
    $qq = intval($_POST['qq']) ;
    $address = trim($_POST['address']) ;
    $logoname = trim($_POST['logoname']) ;
    $preg_match = preg_match("/^[a-z][a-z0-9]{4,14}$/i",$realname);
    $t = time();

    if(!$preg_match){
        alert("您输入的名字格式不正确，请重新输入");
        exit;
    }
    if(!is_phone($phone)){
        alert("手机号码格式不正确，请重新输入");
        exit;
    }
    if(!is_email($mail)){
        alert("电子邮箱格式不正确，请重新输入");
        exit;
    }
    $match_qq = preg_match("/^[1-9][0-9]{4,10}$/",$qq);
    if(!$match_qq){
        alert("QQ格式不正确，请重新输入");
        exit;
    }

    if(isset($_FILES)){
        if($_FILES['photo']['error']==0){
            $filename = file_ext($_FILES['photo']['name']);
            $logoname = $t.rand(10000,99999).$filename;
            $path = $user_logo_up.$logoname;
            $upload = move_uploaded_file($_FILES['photo']['tmp_name'],$path);
        }
    }
    $sql = "UPDATE user SET realname='$realname',sex='$sex',birthday='$birthday',mobile='$phone',email='$mail',qq='$qq',address='$address',logo='$logoname' WHERE userid=".$_COOKIE['userid'];
    $result = $db->query($sql) ;
    if($result){
        alert("信息修改成功","user_center.php");
        exit;
    }else{
        alert("信息修改失败");
        exit;
    }
}

$smarty->display("user_setting.tpl");
$db->close();
?>

